/**
 * 安全奖品库存记录 编辑页
 *
 * 作者 admin
 * 日期 2024-08-06 09:41:00
 */
layui.use(['jquery','febs', 'form', 'validate','commonJS', 'fileUpload'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        commonJS=layui.commonJS,
        $view = $('#baseGoodsInventoryRecord-edit'),
        validate = layui.validate,
        fileUpload = layui.fileUpload;



    form.verify(validate);
    form.render();
    initFormValue();
    initGoodsSelect();


    // imgView = function(obj){
    //     $view.find(".big-img-div img").attr("src", ctx+$view.find("option:selected").attr('data-filePath'));
    // }
    //
    // $view.find(".big-img-div img").load(function () {
    //     commonJS.showImgBig($view,this,this.width,this.height);
    // });
    //
    // $view.find(".big-img-div").click(function () {//再次点击淡出消失弹出层
    //     $(this).fadeOut("fast");
    // });

    function initGoodsSelect() {
        var data ={
            tenantId:POPUP_DATA.tenantId
        };
        //初始化商品单位
        febs.getSync(ctx + 'baseGoods', data, function (r) {
            // console.log("商品列表" + JSON.stringify(r.data))
            var areaLis = r.data;
            var options = '';
            $.each(areaLis, function (index, item) {
                options += '<option value="' + item.goodsId + '" data-fileName="'+item.fileName+'" ' +
                    'data-smallPath="'+item.smallPath+'" data-filePath="'+item.filePath+'"' +
                    ' data-inventory="'+item.inventory+'" data-lockNumber="'+item.lockNumber+'">' + item.goodsName + '</option>'
            })
            $("#goodsId").append(options);
        });
        form.render();
    }

    form.on('select(goodsId)', function(obj){
        var imgData = [];
        if(obj.value == ''){
            initImg(imgData);
            $('#inventory').text('--')
            $('#lockNumber').text('--')
        }else {
            $('#inventory').text($view.find("option:selected").attr('data-inventory'))
            $('#lockNumber').text($view.find("option:selected").attr('data-lockNumber'))

            imgData.push({
                filePath:$view.find("option:selected").attr('data-filePath'),
                fileName: $view.find("option:selected").attr('data-fileName'),
                smallPath:$view.find("option:selected").attr('data-smallPath')
            })
            initImg(imgData);
        }
        form.render();
    });
    function initFormValue() {
        if (baseGoodsInventoryRecordData) {
        }else {
            form.val("baseGoodsInventoryRecord-edit-form", {
                "tenantId": POPUP_DATA.tenantId,
                "operateType": POPUP_DATA.operateType,
            });
        }
    }

    function initImg(imgData){
        fileUpload.render({
            elem: '#fileUpload',
            url: '',
            type: 0,//0-图片，1-视频，2-其他
            defaultData: imgData,
            isDetail: true,
            maxNum: 1,
            done: function (res) {

            }
        });
    }

    form.on('submit(baseGoodsInventoryRecord-edit-form-submit)', function (data) {
        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        var msg = '确定增加出入库记录？'
        febs.modal.confirm('出入库登记', '', function () {
            febs.postArray(ctx + 'baseGoodsInventoryRecord', data, function () {
                layer.closeAll();
                febs.alert.success('操作成功');
                $('#febs-baseGoodsInventoryRecord').find('#query').trigger('click');
            });
        });

    }
});